#include "mypushbutton.h"
#include <QString>

MyPushButton::MyPushButton(QString normoalImg, QString pressImg)
{
    this->normoalImgPath = normoalImg;
    this->pressImgPath = pressImg;

    QPixmap pix;
    bool ret = pix.load(normoalImg);
    if (!ret)
    {
        QString str = QString("加载 %1 资源失败！").arg(normoalImg);
                          QMessageBox::warning(this, "消息提示", str);
        return;
    }

    //设置图片大小
    this->setFixedSize(pix.width(), pix.height());

    //设置圆的按钮
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图标
    this->setIcon(pix);

    //设置图片大小
    this->setIconSize(QSize(pix.width(), pix.height()));
}

//创建动画用于翻金币时的下翻效果
void MyPushButton::zoom1()
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(200);  //动画间隔
    animation->setStartValue(QRect(this->x(),  this->y(),  this->width(), this->height()));  //弹跳起始位置
    animation->setEndValue(QRect(this->x(),  this->y()+10,  this->width(), this->height())); //弹跳结束位置
    animation->setEasingCurve(QEasingCurve::OutBounce); //设置弹跳动画曲线
    animation->start();  //开始动画

}

//创建动画用于翻金币时的上跳效果
void MyPushButton::zoom2()
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(200);  //动画间隔
    animation->setStartValue(QRect(this->x(),  this->y()+10,  this->width(), this->height()));  //弹跳起始位置
    animation->setEndValue(QRect(this->x(),  this->y(),  this->width(), this->height())); //弹跳结束位置
    animation->setEasingCurve(QEasingCurve::OutBounce); //设置弹跳动画曲线
    animation->start();  //开始动画
}

//按钮的鼠标按下事件
void MyPushButton::mousePressEvent(QMouseEvent *ev)
{
    //鼠标左键事件
    if (Qt::LeftButton == ev->button())  //判断左键按下，切换图片
    {
        if ("" != this->pressImgPath)
        {
            QPixmap pix;
            bool ret = pix.load(this->pressImgPath);
            if (!ret)
            {
                QMessageBox::warning(this, "消息提示", "加载资源失败！");
                return;
            }

            //设置图片大小
            this->setFixedSize(pix.width(), pix.height());
            //设置图标
            this->setIcon(pix);
            //设置图片大小
            this->setIconSize(QSize(pix.width(), pix.height()));
        }
    }

    
    return QPushButton::mousePressEvent(ev);
}

//按钮的鼠标释放事件
void MyPushButton::mouseReleaseEvent(QMouseEvent *ev)
{
    //鼠标左键事件
    if (Qt::LeftButton == ev->button())  //鼠标释放，切换图片
    {
        if ("" != this->normoalImgPath)
        {
            QPixmap pix;
            bool ret = pix.load(this->normoalImgPath);
            if (!ret)
            {
                QMessageBox::warning(this, "消息提示", "加载资源失败！");
                return;
            }

            //设置图片大小
            this->setFixedSize(pix.width(), pix.height());
            //设置图标
            this->setIcon(pix);
            //设置图片大小
            this->setIconSize(QSize(pix.width(), pix.height()));
        }
    }
    
    return QPushButton::mouseReleaseEvent(ev);
}

